package com.first.computerstore.controller;

import com.first.computerstore.service.CartService;
import com.first.computerstore.utils.JsonResult;
import com.first.computerstore.vo.CartVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Description:购物车模块
 * Author: LI
 * Date: 2023/6/30 9:52
 * Version: ${1:1.0.0}
 */


@RestController
@RequestMapping("/cart")
public class CartController extends BaseController{

    @Resource
    private CartService cartService;

    /**购物车数据显示
     * 通过request,获取uid
     * @param request
     * @return
     */
    @GetMapping("/showCarts")
    public JsonResult<List<CartVo>> queryCartProductByUid(HttpServletRequest request){
        //得到用户id  uid
        Integer uid = (Integer) request.getSession().getAttribute("uid");
        //调用service层的方法
        JsonResult<List<CartVo>> jsonResult = cartService.queryCartProductByUid(uid);


        return jsonResult;
    }

    /**
     * 删除购物车数据
     *      获取uid
     *          判断是否是登陆状态
     *          判断uid是否为空，为空不允许进行删除操作
     *      获取购物车id（知道对哪条数据进行删除操作）
     *      传入购物车id，进行删除
     *
     * @param request
     * @return
     */
    @PostMapping("/deleteCart")
    public JsonResult<Integer> deleteProductFromCart(String cids,HttpServletRequest request) {
        //得到uid
        Integer uid = (Integer) request.getSession().getAttribute("uid");

        cartService.deleteProductFromCart(uid,cids);

        return new JsonResult<>(200);
    }

    /**
     * 修改商品数量
     * @param cid
     * @param num
     * @return
     */
    @PostMapping("/updateCart")
    public JsonResult<Integer> updateCartNum(Integer cid,Integer num,HttpServletRequest request){
        //得到uid
        Integer uid = (Integer) request.getSession().getAttribute("uid");

        JsonResult<Integer> jsonResult = cartService.updateCartNum(cid,num,uid);

        return jsonResult;
    }

    /**
     * 添加商品进购物车
     * @param pid
     * @param price
     * @param num
     * @return
     */
    @PostMapping("/addCart")
    public JsonResult<Integer> addUserCart(Integer pid, Long price, Integer num, HttpSession session){
        //得到uid
        Integer uid = (Integer) session.getAttribute("uid");

        JsonResult<Integer> jsonResult = cartService.addUserCart(uid,pid,price,num);

        return jsonResult;
    }

    /**
     *
     * @param cids
     * @return
     */
    @GetMapping("/queryCids")
    public JsonResult<List<CartVo>> queryCartsByCids(String cids) {
        List<CartVo> cartVoList = new ArrayList<>();

        String[] tempList = cids.split(",");
        List<Integer> cidList = new ArrayList<>();
        for (String  s: tempList) {
            cidList.add(Integer.valueOf(s));
        }
        for(Integer cid:cidList){
            CartVo cartVo = cartService.queryCartVoByCid(cid);
            cartVoList.add(cartVo);
        }

        return new JsonResult<>(200,cartVoList);
    }
}
